var e = getApp(), t = require("../../fsm.js"), n = (require("../../utils/pingpp.js"), 
require("../../utils/util.js")), o = require("../../nitro.js"), a = function(e) {
    var t = o.getStores();
    return e.forEach(function(e) {
        t.forEach(function(t) {
            if (t.id == e.store_id) {
                e.store = t, e.ticket.inventory.sku_label = n.skuLabel(e.ticket.inventory.sku), 
                e.child_label = "暂无", e.children.map(function(t) {
                    if (t.name || t.nickname) {
                        var n = [];
                        n.push(t.name), n.push(t.nickname), e.child_label = n.join(" / ");
                    }
                });
                var o = [];
                e.order_items.map(function(e) {
                    e.item.options && e.item.options.map(function(e) {
                        o.push(e.title + ": " + e.content);
                    });
                }), e.options_label = o;
            }
        });
    }), e.sort(function(e, t) {
        return +new Date(t.updated_at) - +new Date(e.updated_at);
    });
}, r = [ {
    name: "预订",
    buttonText: "去付款",
    page: "pay"
}, {
    name: "拍摄",
    buttonText: "订单改期",
    page: "/pages/order-update-date/order-update-date"
}, {
    name: "选片",
    buttonText: "去选片",
    page: "/pages/template-select/template-select"
}, {
    name: "后期",
    buttonText: "查看客片",
    page: "/pages/guest-photos/guest-photos"
}, {
    name: "制作",
    buttonText: "查看客片",
    page: "/pages/guest-photos/guest-photos"
}, {
    name: "发货",
    buttonText: "查看客片",
    page: "/pages/guest-photos/guest-photos"
}, {
    name: "评价",
    buttonText: "去评价",
    page: "/pages/guest-photos/guest-photos"
} ];

Page({
    data: {
        loaded: !1,
        isTestEnv: e.globalData.isTestEnv,
        orderList2: null,
        processList: r
    },
    onLoad: function(t) {
        var n = this;
        t.reload && this.setData({
            loaded: !1,
            orderList2: null
        }), wx.showLoading({
            title: ""
        }), e.login(function() {
            n.initOrders();
        });
    },
    onReady: function() {},
    onShow: function() {
        this.data.loaded && this.initOrders();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    initOrders: function() {
        var n = this, o = e.globalData.BASE_URL + "/users/" + e.globalData.userId + "/orders";
        e.getRequest(o).then(function(e) {
            var o = a(e);
            o.forEach(function(e) {
                var o = t.states(e);
                o && (e.mp_status = o, e.mp_status_index = e.mp_status.findIndex(function(e) {
                    return !0 === e.active;
                })), e.mp_actions = n.initOrderActions(e), delete e.historical;
            }), n.setData({
                loaded: !0,
                orderList2: o.slice(0, 50)
            }), wx.hideLoading();
        });
    },
    initOrderActions: function(e) {
        var t = {
            faq: !1,
            cancel: !1,
            reschedule: !1,
            prepay: !1,
            selection: !1,
            confirm_album: !1,
            evaluation: !1,
            revoked: !1
        };
        return "created" === e.state && "unpaid" === e.payment_state && (t.faq = !0, t.prepay = !0), 
        "confirmed" === e.state && "created" === e.ticket.state ? (t.faq = !0, t.reschedule = !0) : "revoked" === e.state ? t.revoked = !0 : "closed" === e.state ? t.closed = !0 : "prepaid" === e.payment_state && (t.faq = !0, 
        t.reschedule = !0), "ee681c9d4db6de2733747ae6" === e.store_id && (t.reschedule = !1), 
        t;
    },
    checkTipsHandler: function() {
        wx.navigateTo({
            url: "/pages/faq/faq"
        });
    },
    openLocationHandler: function(e) {
        var t = e.currentTarget.dataset.storeid, n = o.getStore(t);
        console.info("open map", n.gps_coordinates), wx.openLocation({
            name: n.name,
            address: n.location,
            latitude: n.gps_coordinates[1] - 0,
            longitude: n.gps_coordinates[0] - 0,
            success: function(e) {
                e.latitude, e.longitude;
            }
        });
    },
    jumpHanlder: function(e) {
        e.currentTarget.dataset.index;
        var t = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: t,
            success: function(e) {},
            fail: function(e) {},
            complete: function(e) {}
        });
    },
    wechatContact: function(e) {
        var t = e.currentTarget.dataset.storeid, n = o.getStore(t);
        wx.setClipboardData({
            data: n.wechat,
            success: function(e) {
                wx.showToast({
                    title: "微信已复制",
                    icon: "success",
                    image: "",
                    duration: 0,
                    mask: !0,
                    success: function(e) {},
                    fail: function(e) {},
                    complete: function(e) {}
                });
            },
            fail: function(e) {},
            complete: function(e) {}
        });
    },
    makeCall: function(e) {
        var t = e.currentTarget.dataset.storeid, n = o.getStore(t);
        wx.makePhoneCall({
            phoneNumber: n.phone,
            success: function(e) {},
            fail: function(e) {},
            complete: function(e) {}
        });
    },
    gotoOrderDetail: function(e) {
        var t = e.currentTarget.dataset.orderid;
        wx.navigateTo({
            url: "/pages/order-detail/order-detail?order_id=" + t,
            success: function(e) {},
            fail: function(e) {},
            complete: function(e) {}
        });
    },
    onReschedule: function(t) {
        var o = t.currentTarget.dataset.orderid, a = t.currentTarget.dataset.storeid, r = this.data.orderList2.find(function(e) {
            return e._id === o;
        });
        if (console.info("onReschudle", o, r), r.ticket.inventory && !r.ticket.inventory.id && !r.ticket.inventory.version) return e.showErrorMsg("", "当前订单处于悬挂状态，请联系客服改期。");
        wx.showModal({
            title: "改期说明",
            content: "- 每位预定成功的客人，都会有一次更改档期的机会；\r\n- 修改的档期只能安排工作日拍摄，望谅解；\r\n- 第二次更改档期将扣除定金的50%作为空缺；\r\n- 第三次我们将扣除的50%定金作为档期空缺费用。\r\n \r\n注：离拍摄日期三天内（包含拍摄当天）更改档期将扣除全部定金作为档期空缺的费用。",
            showCancel: !0,
            cancelText: "我要改期",
            cancelColor: "#C2A469",
            confirmText: "不改期",
            confirmColor: "#259B24",
            success: function(t) {
                if (t.cancel) {
                    if (r.ticket.inventory && n.moment(r.ticket.inventory.date).diff(n.moment(), "seconds") < 259200) return e.showErrorMsg("离拍摄日期不足三天", "如有改期需求，请联系微信客服");
                    wx.navigateTo({
                        url: "/pages/reschedule/reschedule?order_id=" + o + "&store_id=" + a,
                        success: function(e) {},
                        fail: function(e) {},
                        complete: function(e) {}
                    });
                }
            },
            fail: function(e) {},
            complete: function(e) {}
        });
    },
    onPayOrder: function(e) {
        var t = e.currentTarget.dataset.orderid;
        o.payOrder(t, function() {
            wx.reLaunch({
                url: "/pages/orders/orders?reload=true"
            });
        }, function() {});
    },
    onCancelOrder: function(t) {
        var n = t.currentTarget.dataset.orderid;
        wx.showModal({
            title: "",
            content: "取消后档期会自动释放，如有拍摄需求需重新预定。",
            showCancel: !0,
            cancelText: "取消",
            cancelColor: "#C2A469",
            confirmText: "确认",
            confirmColor: "#259B24",
            success: function(t) {
                t.confirm && e.postRequest(e.globalData.BASE_URL + "/orders/" + n + "/revoke", {}).then(function(e) {
                    console.info("revoke", e), 200 == e.code && wx.showModal({
                        title: "订单已取消",
                        content: e.message,
                        success: function(e) {
                            wx.reLaunch({
                                url: "/pages/orders/orders?reload=true"
                            });
                        }
                    });
                });
            },
            fail: function(e) {},
            complete: function(e) {}
        });
    },
    onExitTestEnv: function(t) {
        e.markAsDebug(!1);
    }
});